from django.shortcuts import render, redirect,HttpResponse
from django.http import JsonResponse
from .models import Detail
from .forms import DetailForm

# Create your views here.

# 首页视图函数
def index(request):
    return render(request,'index.html')

def detail(request):
    if request.method == "GET":
        queryset = Detail.objects.all()
        forms = DetailForm()
        context = {
            "queryset":queryset,
            "forms":forms,
        }
    

        return render(request,'detail.html',context)

    form = DetailForm(data=request.POST)
    if form.is_valid():
        form.save()
        return JsonResponse({'success': True})
    # errors=form.errors.as_data()
    # error_messages = [str(value) for key, value in errors.items() for value in value]
    return JsonResponse({'success':False,"errors":form.errors})
    


